import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayDemo {
    public static void main(String[] args) {
        int[] scores = { 70, 76, 60, 85, 85, 85};
        Arrays.sort(scores);
        printArray(scores);
        int targetIndex = Arrays.binarySearch(scores, 76);
        System.out.println(targetIndex);

        int[] testArr = new int[10];
        Arrays.fill(testArr, 100);
        printArray(testArr);


    }

    public static void arrayDefine() {
        int[] arrI = new int[6];
        float[] arrF = new float[6];
        double[] arrD = new double[6];
        //对于布尔类型的默认值为false
        boolean[] arrB = new boolean[6];
        char[] arrC = new char[6];
        //对于引用类型的默认值为null
        String[] arrS = new String[6];

        System.out.println("int:" + arrI[1]);
        System.out.println("float:" + arrF[0]);
        System.out.println("double:" + arrD[2]);
        System.out.println("boolean:" + arrB[3]);
        System.out.println("char:" + arrC[4]);
        System.out.println("String:" + arrS[5]);
    }

    /*
    二分查找
    1、前提是查找的数组必须是有序的
     */
    public static void binarySearch(int[] scores) {
        popSort(scores);
        int left = 0;
        int right = scores.length - 1;
        int target = 76;

        // 二分查找
        //1.数据一定是有序的
        while (true) {
            int mid = (right + left) / 2;

            if (scores[mid] > target) {
                left = mid + 1;
            } else if (scores[mid] < target) {
                right = mid - 1;
            } else {
                System.out.println("找到了目标值，下标为："+ mid);
                break;
            }
        }
    }

    public static void printArray(int[] scores) {
        System.out.println(Arrays.toString(scores));
    }

    public static void popSort(int[] scores) {

        for (int i = 0; i < scores.length; i++) {
            int sign = 0;
            for (int j = 0; j < scores.length - 1; j ++) {
                if (scores[j] < scores[j+1]) {
                    sign++;
                    int tmp = scores[j];
                    scores[j] = scores[j+1];
                    scores[j+1] = tmp;
                }
            }

            if (sign == 0) {
                break;
            }
        }

    }

    public static void maxScore() {
        int[] scores = new int[5];
        scores[0] = 70;
        scores[1] = 80;
        scores[2] = 76;
        scores[3] = 60;
        scores[4] = 85;

        int maxScore = scores[0];
        for (int i = 1; i < scores.length; i++) {
            if (scores[i] > maxScore) {
                maxScore = scores[i];
            }
        }

        System.out.println("最高分为：" + maxScore);
    }



    public static void arrGet() {
        int[] scores = new int[5];
        scores[0] = 70;
        scores[1] = 80;
        scores[2] = 76;
        scores[3] = 60;
        scores[4] = 85;

        int sumScore = 0;
        for (int score : scores) {
            sumScore += score;
        }

        double average = sumScore / (double)scores.length;

        System.out.println("平均成绩为：" + average);
    }
}
